﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 花店管理系统.Tproduct
{
    public partial class Delete_product : Form
    {
        public Delete_product()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProudct, daLog;
        DataSet ds = new DataSet();

        void init()
        {
            DB.GetCn();
            string str = "select * from Tgood";
            string sdr = "select *from log";
            daProudct = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daProudct.Fill(ds, "product_info");
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }

        private void Delete_product_Load(object sender, EventArgs e)
        {
            AddSelectionColumn();
            init();
            showAll();
        }

        private void AddSelectionColumn()
        {
            showXz();
        }

        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }

        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dgvProduct.Rows.Count>0)
            {
                for(int i=0;i<dgvProduct.Rows.Count;i++)
                {
                    DataGridViewCheckBoxCell ck = dgvProduct.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i!=e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
              
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int s = dgvProduct.Rows.Count;
            for(int i=0;i<dgvProduct.Rows.Count;i++)
            {
                if (IsRowSelected(i))
                {
                    DialogResult dr = MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        dgvProduct.Rows.RemoveAt(i);
                        //添加日志
                        DataRow drLog = ds.Tables["log_info"].NewRow();
                        drLog["tname"] = Login.username;
                        drLog["L_type"] = "删除";
                        drLog["L_date"] = DateTime.Now;
                        drLog["L_table"] = "Tgood表";
                        ds.Tables["log_info"].Rows.Add(drLog);

                        UpdateDB();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    s = s - 1;

                }
            }
            if (s==0)
            {
                MessageBox.Show("请选择要删除的项");
            }
        }

        private bool IsRowSelected(int i)
        {
            return dgvProduct.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True";
        }

        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProudct);
                daProudct.Update(ds, "product_info");
                SqlCommandBuilder dbLog = new SqlCommandBuilder(daLog);
                daLog.Update(ds, "log_info");
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvProduct.Columns.Add(acCode);
        }
    }
}
